const {
  createEnroll,
  findByPageEnroll,
  updateEnroll,
} = require("../controller/enrollController");
const { validateFunc } = require("../utils/tools");
const { ValidationError } = require("../utils/customError");

// 报名登记
exports.addEnroll = async (enrollInfo) => {
  const enrollRule = {
    name: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
    age: {
      presence: {
        allowEmpty: false,
      },
      type: "integer",
    },
    sex: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "integer",
    },
    email: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
    phone: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
    hobby: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
    introduction: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
    recruitInfo: {
      presence: {
        // 是否为空
        allowEmpty: false,
      },
      type: "string",
    },
  };

  try {
    await validateFunc(enrollRule, enrollInfo);
    enrollInfo.status = 0;
    return createEnroll(enrollInfo);
  } catch (e) {
    console.log(e);
    throw new ValidationError("数据验证失败");
  }
};

// 分页获取报名记录
exports.getEnroll = (query) => {
  return findByPageEnroll(query);
};

// 修改报名信息
exports.editEnroll = (id, enrollInfo) => {
  return updateEnroll(id, enrollInfo);
};
